package dao

import (
	"fanqiechaodan-blog/models"
	"go.uber.org/zap"
)

func PostPage(page, pageSize int) ([]models.Post, error) {
	var res []models.Post
	// 计算可以使用的page
	page = (page - 1) * pageSize
	err := DB.SelectList("SELECT * FROM blog_post ORDER BY create_at DESC LIMIT ?,? ", &res, page, pageSize)
	return res, err
}

func PostPageByCategoryId(cid, page, pageSize int) ([]models.Post, error) {
	var res []models.Post
	// 计算可以使用的page
	page = (page - 1) * pageSize
	err := DB.SelectList("SELECT * FROM blog_post WHERE category_id = ? ORDER BY create_at DESC LIMIT ?,? ", &res, cid, page, pageSize)
	return res, err
}

func PostCount() (int, error) {
	sql := "SELECT COUNT(1) FROM blog_post"
	row := DB.QueryRow(sql)
	if row.Err() != nil {
		zap.S().Errorf("执行sql失败;sql:%s,err:%s", sql, row.Err().Error())
		return 0, row.Err()
	}
	var res int
	err := row.Scan(&res)
	if err != nil {
		zap.S().Errorf("Scan值失败;sql:%s,err:%s", sql, err.Error())
		return 0, row.Err()
	}
	return res, nil
}

func PostCountByCategoryId(cid int) (int, error) {
	sql := "SELECT COUNT(1) FROM blog_post WHERE category_id = ? "
	row := DB.QueryRow(sql, cid)
	if row.Err() != nil {
		zap.S().Errorf("执行sql失败;sql:%s,cid: %d,err:%s", sql, cid, row.Err().Error())
		return 0, row.Err()
	}
	var res int
	err := row.Scan(&res)
	if err != nil {
		zap.S().Errorf("Scan值失败;sql:%s,err:%s", sql, err.Error())
		return 0, row.Err()
	}
	return res, nil
}

func PostPageBySlug(slug string, page, pageSize int) ([]models.Post, error) {
	var res []models.Post
	// 计算可以使用的page
	page = (page - 1) * pageSize
	err := DB.SelectList("SELECT * FROM blog_post WHERE slug = ? ORDER BY create_at DESC LIMIT ?,? ", &res, slug, page, pageSize)
	return res, err
}

func PostCountBySlug(slug string) (int, error) {
	sql := "SELECT COUNT(1) FROM blog_post WHERE slug = ?"
	row := DB.QueryRow(sql, slug)
	if row.Err() != nil {
		zap.S().Errorf("执行sql失败;sql:%s,slug:%s,err:%s", sql, slug, row.Err().Error())
		return 0, row.Err()
	}
	var res int
	err := row.Scan(&res)
	if err != nil {
		zap.S().Errorf("Scan值失败;sql:%s,slug:%s,err:%s", sql, slug, err.Error())
		return 0, row.Err()
	}
	return res, nil
}

func GetPostDetailById(pid int) (models.Post, error) {
	var res models.Post
	err := DB.SelectOne(&res, "SELECT * FROM blog_post WHERE pid = ?", pid)
	return res, err
}

func GetAllPost() ([]models.Post, error) {
	var res []models.Post
	err := DB.SelectList("SELECT pid,title,create_at FROM blog_post", &res)
	if err != nil {
		return nil, err
	}
	return res, nil
}

func GetPostByCondition(condition string) ([]models.Post, error) {
	var res []models.Post
	err := DB.SelectList("SELECT pid,title FROM blog_post WHERE title LIKE ? ORDER BY create_at DESC", &res, "%"+condition+"%")
	return res, err
}

func SavePost(post *models.Post) error {
	sql := "INSERT INTO blog_post(title,content,markdown,category_id,user_id,view_count,type,slug,create_at,update_at)VALUES (?,?,?,?,?,?,?,?,?,?)"
	res, err := DB.Exec(sql, post.Title, post.Content, post.Markdown, post.CategoryId, post.UserId, post.ViewCount, post.Type, post.Slug, post.CreateAt, post.UpdateAt)
	if err != nil {
		zap.S().Errorf("执行SQL错误;sql:%s,post:%+v,err:%s", sql, post, err.Error())
		return err
	}
	id, _ := res.LastInsertId()
	post.Pid = int(id)
	return nil
}

func UpdatePost(post *models.Post) error {
	sql := "UPDATE blog_post SET title = ?,content = ?,markdown = ?,category_id = ?,user_id = ?,type = ?,slug=?,update_at = ? WHERE pid = ?"
	_, err := DB.Exec(sql,
		post.Title,
		post.Content,
		post.Markdown,
		post.CategoryId,
		post.UserId,
		post.Type,
		post.Slug,
		post.UpdateAt,
		post.Pid)
	if err != nil {
		zap.S().Errorf("执行SQL错误;sql:%s,post:%+v,err:%s", sql, post, err.Error())
		return err
	}
	return nil
}

func UpdatePostViewCount(pid int) error {
	sql := "UPDATE blog_post SET view_count = view_count+1 WHERE pid = ?"
	_, err := DB.Exec(sql, pid)
	if err != nil {
		zap.S().Errorf("执行SQL错误;sql:%s,pid:%d,err:%s", sql, pid, err.Error())
		return err
	}
	return nil
}

func DeletePost(pid int) error {
	sql := "DELETE FROM blog_post WHERE pid = ?"
	_, err := DB.Exec(sql, pid)
	if err != nil {
		zap.S().Errorf("执行SQL错误;sql:%s,pid:%d,err:%s", sql, pid, err.Error())
		return err
	}
	return nil
}
